var h = $(window).height(),
    lakssj = '', //立案时间开始
    lajssj = '', //立案时间结束
    lrkssj = '', //录入时间开始
    lrjssj = '', //录入时间结束
    dalb = '',   //大案类型
    types = 0,     //三种方式识别
    kinds = null, //某一方式数据
    area = [],
    hasAttachData = [],
    noAttachData = [],
    case_1 = '',   //大案类型数据
    case_2 = '',    //分局类型数据
    case_3 = '',    //单位类型数据
    case_4 = '',    //用户类型数据
    myChart = echarts.init(document.getElementById('echart'));

$(window).ready(function(){
     h = $(window).height();
    $('.caseContent').css('height',h - 20);
    //日期
    laydate.render({
        elem: '.caseSlot',
        type: 'date',
        range: '~',
        theme: 'molv',//主题
        min: '1900-1-1 00:00:00',
        max: 0,
        format: 'yyyy-MM-dd',
        done: function(value, date, endDate){
            if(value != ''){
                lakssj = value.split('~')[0];
                lajssj = value.split('~')[1];
            }else{
                lakssj = '';
                lajssj = '';
            }
        }
    });
    laydate.render({
        elem: '.timeSlot',
        type: 'date',
        range: '~',
        theme: 'molv',//主题
        min: '1900-1-1 00:00:00',
        max: 0,
        format: 'yyyy-MM-dd',
        done: function(value, date, endDate){
            if(value != ''){
                lrkssj = value.split('~')[0];
                lrjssj = value.split('~')[1];
            }else{
                lrkssj = '';
                lrjssj = '';
            }
        }
    });
    //日期结束
    $(window).resize(function(){
        myChart.resize();
        h = $(window).height();
        $('.caseContent').css('height',h - 20);
    })
//初始数据加载
    $.ajax({
        type: 'get',
        url: baseUrls + 'count/caseTj?type=0',
        data: '',
        beforeSend: function(){
            Loading();
        },
        success: function(response){
            closeLoading();
            if(response.responseCode == '1000'){
                for (var i = 0; i < response.content.nFileUnitCount.length; i++){
                    area.push(response.content.nFileUnitCount[i].lxmc);
                    hasAttachData.push(
                        {
                            value: response.content.hFileUnitCount[i].lxsl,
                            itemStyle: { normal: {color: '#036ac7', label:{textStyle:{color:'white'}}}}
                        }
                    );
                    noAttachData.push(
                        {
                            value: response.content.nFileUnitCount[i].lxsl,
                            itemStyle: { normal: {color: '#43b1ef', label:{textStyle:{color:'white'}}}}
                        }
                    )
                };
                Echart(area,hasAttachData,noAttachData);
            } else {
                Msg();
            }
        },
        error: function(){
            closeLoading();
            Msg();
        }
    })
})
// echart
function Echart(address,has,no){
    // var add = [];
    // for(var i = 0; i < address.length; i++){
    //     if(address[i].indexOf('局') >= 0 && address[i].length > 6){
    //         var Index = address[i].indexOf('局');
    //         add.push(address[i].substring((Index + 1)));
    //     }else{
    //         add.push(address[i]);
    //     }
    // }
    var option = {
        tooltip : {
            trigger: 'axis',
            axisPointer : {            // 坐标轴指示器，坐标轴触发有效
                type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
            }
        },
        toolbox: {
            show: true,
            feature: {
                mark: {show: false},
                dataView: {
                    show: true,
                    title: '数据列表'
                },
                restore: {},
                saveAsImage: {
                    show: true,
                    title: '下载'
                }
            }
        },
        legend: {
            data: [
                {
                    name: '案件(含附件信息)',
                    textStyle:{color:'#036ac7'}
                },
                {
                    name:'案件(仅文字线索)',
                    textStyle:{color:'#036ac7'}
                }
            ]
        },
        color: ['#036ac7', '#43b1ef'],
        grid: {
            left: '20px',
            right: '20px',
            bottom: '6%',
            containLabel: true,
            borderWidth: 1,
            borderColor: 'transparent'
        },
        xAxis:  {
            type: 'category',
            data: address,
            axisLine:{
                lineStyle:{
                    color:'#51679a',
                    width:1
                }
            },
            axisLabel : {
                interval: 0,
                textStyle: {
                    color: '#03b5ff'
                },
                formatter: function (params) {
                    var newParamsName = "";
                    var paramsNameNumber = params.length;
                    var provideNumber = 6;
                    var rowNumber = Math.ceil(paramsNameNumber / provideNumber);
                    if (paramsNameNumber > provideNumber) {
                        for (var p = 0; p < rowNumber; p++) {
                            var tempStr = "";
                            var start = p * provideNumber;
                            var end = start + provideNumber;
                            if (p == rowNumber - 1) {
                                tempStr = params.substring(start, paramsNameNumber);
                            } else {
                                tempStr = params.substring(start, end) + "\n";
                            }
                            newParamsName += tempStr;
                        }

                    } else {
                        newParamsName = params;
                    }
                    return newParamsName
                }
            }
        },
        yAxis: {
            type: 'value',
            //参考线设置
            splitLine: {
                show: true,
                lineStyle:{
                    color: ['#51679a']
                }
            },
            axisLine:{
                lineStyle:{
                    color:'#51679a',
                    width:1
                }
            },
            axisLabel:{
                textStyle: {
                    color: '#03b5ff'
                }
            }
        },
        series: [
            {
                name: '案件(含附件信息)',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                barWidth: 100,
                data: has
            },
            {
                name: '案件(仅文字线索)',
                type: 'bar',
                stack: '总量',
                label: {
                    normal: {
                        show: true,
                        position: 'insideRight'
                    }
                },
                barWidth: 10,
                data: no
            }
        ]
    };
    myChart.setOption(option);
    myChart.on('click',function(params){
        var Name = params.name;
        dalb = case_1;
        $.ajax({
            type: 'get',
            url: baseUrl + 'count/caseJq',
            data: {
                lakssj: lakssj,
                lajssj: lajssj,
                lrkssj: lrkssj,
                lrjssj: lrjssj,
                dalb: dalb,
                type: types,
                kind: Name,
                pageIndex: 0,
                pageSize: 10
            },
            beforeSend: function(){
                Loading();
            },
            success: function(response){
                closeLoading();
                $('.caseTable').css('display','block');
                if(response.responseCode == '1000'){
                    if(response.content.list && response.content.list.length > 0){
                        $("#ul-content").html('');
                        var state = '';
                        for(var i = 0; i< response.content.list.length; i++){
                            if(response.content.list[i].fjxx == '0'){
                                state = '无';
                            }else{
                                state = '有';
                            }
                            $("#ul-content").append(' <li><span onclick="showDetail(\''+ nulls(response.content.list[i].ajbh) +'\')">' + nulls(response.content.list[i].ajbh) + '</span><span>' + response.content.list[i].ajmc + '</span><span>' + nulls(response.content.list[i].afkssj) + '</span><span>' + state + '</span></li>')
                        }
                        $('.total').html('共'+ nulls(response.content.total) + '项案件');
                        $('.clueList').css('display','block');

                        $('#pagination1').bs_pagination({
                            totalPages: response.content.pages,
                            visiblePageLinks: 10,
                            currentPage: response.content.pageNum,
                            showGoToPage: false,
                            showRowsPerPage: false,
                            showRowsInfo: false,
                            showRowsDefaultInfo: false,
                            navListContainerClass: 'col-xs-12',
                            onChangePage: function (event, data) {
                                dalb = case_1;
                                $.ajax({
                                    type: 'get',
                                    url: baseUrl + 'count/caseJq',
                                    data: {
                                        lakssj: lakssj,
                                        lajssj: lajssj,
                                        lrkssj: lrkssj,
                                        lrjssj: lrjssj,
                                        dalb: dalb,
                                        type: types,
                                        kind: Name,
                                        pageIndex: data.currentPage,
                                        pageSize: 10
                                    },
                                    beforeSend: function(){
                                        Loading();
                                    },
                                    success: function(response){
                                        closeLoading();
                                        if(response.responseCode == '1000'){
                                            $('.caseTable').css('display','block');
                                            if(response.content.list && response.content.list.length > 0){
                                                $("#ul-content").html('');
                                                for(var i = 0; i< response.content.list.length; i++){
                                                    if(response.content.list[i].fjxx == '0'){
                                                        state = '无';
                                                    }else{
                                                        state = '有';
                                                    }
                                                    $("#ul-content").append(' <li><span>' + response.content.list[i].ajbh + '</span><span>' + response.content.list[i].ajmc + '</span><span>' + nulls(response.content.list[i].afkssj) + '</span><span>' + state + '</span></li>')
                                                }
                                                $('.clueList').css('display','block');
                                            } else{
                                                $('.clueList').css('display','none');
                                                Msg('暂无数据');
                                            }
                                        }else{
                                            Msg();
                                        }
                                    },
                                    error: function(){
                                        closeLoading();
                                        Msg();
                                    }
                                })
                            }
                        })
                    } else {
                        $('.clueList').css('display','none');
                        Msg('暂无数据');
                    }
                } else {
                    $('.clueList').css('display','none');
                    Msg();
                }
            },
            error: function(){
                closeLoading();
                Msg();
            }
        })
    })
}
//分局、派出所、用户切换
function changeSearchObj(item){
    var index = $(item).index();
    types = index;
    $(item).addClass('spanActive').removeClass('spanNormal').siblings().addClass('spanNormal').removeClass('spanActive');
    $('.selects .select').css('display','none');
    $('.selects .select').eq(index).css('display','block');
}
//提交
function Submit(){
    setTimeout(function(){
        $('.caseTable').css('display','none');
        var allKinds = [];
        allKinds.push(case_2);
        allKinds.push(case_3);
        allKinds.push(case_4);
        dalb = case_1;
        kinds = allKinds[types] ? allKinds[types].join(',') : '';

        $.ajax({
            type: 'get',
            url: baseUrl + 'count/caseTj',
            data: {
                lakssj: lakssj,
                lajssj: lajssj,
                lrkssj: lrkssj,
                lrjssj: lrjssj,
                dalb: dalb,
                type: types,
                kinds: kinds
            },
            beforeSend: function(){
                Loading();
            },
            success: function(response){
                closeLoading();
                if(response.responseCode == '1000'){
                    area = [];
                    hasAttachData = [];
                    noAttachData = [];
                    var l = response.content.nFileUnitCount.length;
                    $('#echart').show();
                        for (var i = 0; i < response.content.nFileUnitCount.length; i++){
                            area.push(response.content.nFileUnitCount[i].lxmc);
                            hasAttachData.push(
                                {
                                    value: response.content.hFileUnitCount[i].lxsl,
                                    itemStyle: { normal: {color: '#036ac7', label:{textStyle:{color:'white'}}}}
                                }
                            );
                            noAttachData.push(
                                {
                                    value: response.content.nFileUnitCount[i].lxsl,
                                    itemStyle: { normal: {color: '#43b1ef', label:{textStyle:{color:'white'}}}}
                                }
                            )
                        };
                       Echart(area,hasAttachData,noAttachData);
                    if(l > 15){
                        $('#echart').css('width',l * 120);
                        myChart.resize();
                    } else{
                        $('#echart').css('width','100%');
                        myChart.resize();
                    }
                } else {
                    console.log(1);
                }
            },
            error:function(){
                closeLoading();
            }
        })
    },500)
}
//导出
function excel(){
    var allKinds = [];
    allKinds.push(case_2);
    allKinds.push(case_3);
    allKinds.push(case_4);
    dalb = case_1;
    kinds = allKinds[types] ? allKinds[types].join(',') : '';
    $.ajax({
        type: 'post',
        url: baseUrls + 'count/export',
        data: {
            lakssj: lakssj,
            lajssj: lajssj,
            lrkssj: lrkssj,
            lrjssj: lrjssj,
            dalb: dalb,
            type: types,
            kinds: kinds
        },
        beforeSend: function () {
            Loading();
        },
        success: function (response) {
            closeLoading();
            if(response.responseCode == '1000'){
                window.location.href = response.content;
            }else{
                Msg();
            }
        },
        error: function(){
            closeLoading();
            Msg();
        }
    })
}
//四个类型防止重选
function iframeShow(item,num){
if($(item).val().length > 0){
//询问框
    layer.confirm('您已选择，确认重选？', {
        btn: ['取消','确认'] //按钮
    }, function(){
        layer.msg('已取消', {icon: 1});
    }, function(){
        iframeBox(item,num);
    });
}else{
    iframeBox(item,num);
}
}
//获取
function iframeBox(item,num){
    var title = '';
    if(num == 1){
        title = '大案类别';
    }else if(num == 2){
        title = '分局';
    }else if(num == 3){
        title = '派出所';
    }else{
        title = '用户';
    }
    layer.open({
        title: title,
        type: 2,
        area: ['495px','442px'],
        fixed: false, //不固定
        maxmin: false,
        content: 'common/multSel/checkbox_'+ num +'.html',
        cancel: function(index,layero){
            var p = $(layero).find('iframe')[0].contentWindow.p;
            if(num == 1){
                if(p.length > 0){
                    case_1 = p;
                    $(item).val('共选择'+ p.length +'项');
                }else{
                    case_1 = '';
                    $(item).val('');
                }
            }else if(num == 2){
                if(p.length > 0){
                    case_2 = p;
                    $(item).val('共选择'+ p.length +'项');
                }else{
                    case_2 = '';
                    $(item).val('');
                }
            }else if(num == 3){
                if(p.length > 0){
                    case_3 = p;
                    $(item).val('共选择'+ p.length +'项');
                }else{
                    case_3 = '';
                    $(item).val('');
                }
            }else{
                if(p.length > 0){
                    case_4 = p;
                    $(item).val('共选择'+ p.length +'项');
                }else{
                    case_4 = '';
                    $(item).val('');
                }
            }
        }
    });
}
//详情
function showDetail(uniq){
    layer.open({
        type: 2,
        area: ['90%', h * 0.9 + 'px'],
        fixed: false, //不固定
        maxmin: false,
        content: 'layer/layer.html?' + uniq + '&' + $('#userId').val()
    });

}